# -*- coding: utf-8 -*-
# @Time : 2022/2/7 11:46
from Common.mysql import db
from Common.public_method import utc_8
from Common.yaml_method import YamlMethod
from src.ApiTest.ProjectConfig.Database.baseInfo_database import BaseInfoConfig


class AddBaseInfo:
    """
    新增信息
    """
    @staticmethod
    def add_base_info(system_name, project_id, project_name, base_url_test, base_url_develop, base_url_release, header, create_user):
        """
        新增URL信息
        :param system_name: 系统名称
        :param project_id: 项目ID
        :param project_name: 项目名称
        :param base_url_test: 测试环境base URL
        :param base_url_develop: 开发环境base URL
        :param base_url_release: 正式环境base URL
        :param header: 请求头
        :param create_user: 创建人
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        info = BaseInfoConfig.query.filter_by(systemName=system_name, projectId=project_id).first()
        if info is None:
            create_time = utc_8()
            # 项目信息插入数据库
            info = BaseInfoConfig(systemName=system_name, projectId=project_id, projectName=project_name,
                                  base_url_test=base_url_test, base_url_develop=base_url_develop,
                                  base_url_release=base_url_release, header=header, create_time=create_time, create_user=create_user)
            db.session.add(info)
            db.session.commit()

            res = {
                'code': code[0],
                'message': 'success',
                'data': {}
            }
            return res
        else:
            res = {
                'code': code[1],
                'message': '配置已存在',
                'data': {}
            }
            return res
